//plotting enrollment and hours on the same event study plot for the substitution margin analysis

global main "[YOUR PATH HERE]"
global output "${main}/output"


//enrollment type switches
local switch_all = 0
local switch_nponly  = 0
local switch_fponly  = 0
	local switch_ftonly  = 0
	local switch_ptonly  = 0
	local switch_pubonly = 1
	local switch_prionly = 0
	local switch_menonly = 0
	local switch_womenonly = 0
	local switch_msu = 0
	local switch_urban = 0
	local switch_pre08 = 0
	local switch_post08 = 0
	local switch_post00 = 0
	
local switch_controls = 1 //adds controls for free college (dummies) and for the unemployment rate
local switch_balance = 0 // this keeps only a balanced panel of states

local controlsfe "i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost i.event#control_fedeventpost i.event#control_othereventpost"
local clustervar dmw_statefips


//graphing style things
	grstyle init
	grstyle set plain
	grstyle set size 8pt: subheading axis_title


********************************************************************************
**GED DATA DATA**
********************************************************************************
use "${main}/data/clean_GED_long.dta", clear

gen log_completers = ln(completers)
gen log_passers    = ln(passers)

drop if year_academic == 1987 // we have 1987 as a one-off year so we drop

if `switch_balance'{
	egen balance =  total(inrange(year_academic, 1991, 2019)), by(statefips)
	summ balance 
		local bal = r(max)
		
	keep if balance == `bal'
	
	local bal "_bal"
	

}

/*
sort statefips year
	graph twoway (line completers year) (line passers year), ///
		legend(col(2) order(1 "Completers" 2 "Passers")) by(statefips)
		graph export ${output}/ged_counts_bystate`bal'.pdf, replace
*/

preserve
	collapse (sum) completers passers (count) statefips , by(year)
	
	graph twoway (line completers year) (line passers year), ///
		legend(col(2) order(1 "Completers" 2 "Passers"))
		graph export ${output}/ged_counts`bal'.pdf, replace
	
restore



//merging with stacked data 
merge 1:m statefips year_academic using "${main}/data/mw_stacked.dta", keep(match) nogen

	save "${main}/data/mw_stacked_GED.dta", replace	

********************************************************************************
**ENROLLMENT DATA**
********************************************************************************
use "${main}/data/outcomes_enrollment.dta", clear

if `switch_nponly'{
	drop if control == 3
	local type  "_nponly`type'"
}
if `switch_fponly'{
	keep if control == 3
	local type  "_fponly`type'"
}
if `switch_ftonly'{
	keep if efalevel == 22
	local type  "_FT`type'"
}
if `switch_ptonly'{
	keep if efalevel == 42
	local type  "_PT`type'"
}
if `switch_pubonly'{
	keep if control == 1
	local type  "_public`type'"
}
if `switch_prionly'{
	keep if control == 2
	local type  "_private`type'"
}
if `switch_menonly'{
	replace eftotlt = eftotlm
	local type  "_menonly`type'"
}
if `switch_womenonly'{
	replace eftotlt = eftotlw
	local type  "_womenonly`type'"
}
if `switch_msu'{
	keep if msu
	local type  "_msu`type'"
}
if `switch_urban'{
	keep if urban
	local type  "_urban`type'"
}
if `switch_pre08'{
	keep if file_year < 2008
	local type  "_pre08`type'"
}
if `switch_post08'{
	keep if file_year > 2008
	local type  "_post08`type'"
}
if `switch_post00'{
	keep if file_year >= 2000
	local type  "_post00`type'"
}
else if `switch_all'{
	local type = "_all"
}

//collapse by state
collapse (sum) eftotlt , by(file_year deg st_fips)
	rename eftotlt eftotl
	drop if st_fips > 56 //this drops all of the US territories
	drop if file_year < 1986 // this drops 1980, 1984 and 1985, for which our data are much spottier
	
if `switch_controls'{
	//generate indicators for if a state has a free CC program in a given year
	gen free_cc = 0
		replace free_cc = 1 if st_fips == 5  & file_year > 2016
		replace free_cc = 1 if st_fips == 6  & file_year > 2018
		replace free_cc = 1 if st_fips == 9  & file_year > 2019
		replace free_cc = 1 if st_fips == 10 & file_year > 2005
		replace free_cc = 1 if st_fips == 13 & file_year > 2013
		replace free_cc = 1 if st_fips == 15 & file_year > 2017
		replace free_cc = 1 if st_fips == 18 & file_year > 1990
		replace free_cc = 1 if st_fips == 19 & file_year > 2019
		replace free_cc = 1 if st_fips == 20 & file_year > 2016
		replace free_cc = 1 if st_fips == 21 & file_year > 2016
		replace free_cc = 1 if st_fips == 22 & file_year > 2016
		replace free_cc = 1 if st_fips == 24 & file_year > 2016
		replace free_cc = 1 if st_fips == 28 & file_year > 2016
		replace free_cc = 1 if st_fips == 29 & file_year > 2016
		replace free_cc = 1 if st_fips == 30 & file_year > 2016
		replace free_cc = 1 if st_fips == 32 & file_year > 2016
		replace free_cc = 1 if st_fips == 34 & file_year > 2016
		replace free_cc = 1 if st_fips == 36 & file_year > 2016
		replace free_cc = 1 if st_fips == 40 & file_year > 2016
		replace free_cc = 1 if st_fips == 41 & file_year > 2016
		replace free_cc = 1 if st_fips == 44 & file_year > 2016
		replace free_cc = 1 if st_fips == 46 & file_year > 2016
		replace free_cc = 1 if st_fips == 47 & file_year > 2016
		replace free_cc = 1 if st_fips == 53 & file_year > 2016
		replace free_cc = 1 if st_fips == 54 & file_year > 2016
		
	//add controls for unemployment rate by state
	preserve
		use "${main}/data/clean_controls_labormarket.dta", clear
		
		keep statefips unemp_rate year
			rename statefips st_fips
			rename year file_year
		
		tempfile unemployment
		save `unemployment'
	restore 
	
	merge m:1 file_year st_fips using `unemployment', assert(match using) keep(match) nogen 
		//need to assert match master so when we restrict years we can get rid of those not in our sample
		
	//add controls for log state population (18-30 years old)
	preserve
		use "${main}/data/census_yearly_statepop.dta", clear
			rename statefips st_fips
			rename year_academic file_year
		
		tempfile statepop
		save `statepop'
	restore 
	
	merge m:1 file_year st_fips using `statepop', assert(match using) keep(match) nogen 
		//need to assert match master so when we restrict years we can get rid of those not in our sample
	
	rename file_year year_academic
	rename st_fips statefips 
	keep year_academic statefips deg eftotl free_cc unemp_rate lnpop_1830
}
else{	
	rename file_year year_academic
	rename st_fips statefips 
	keep year_academic statefips deg eftotl
}
		
reshape wide eftotl , i(year_academic statefips) j(deg)
	gen log_eftotl1 = ln(eftotl1)
	gen log_eftotl3 = ln(eftotl3)
			
tempfile enroll
save `enroll'

local weights1  ""
local weights3  ""

local ytitle "log enrollment in college"


merge 1:m year_academic statefips using "${main}/data/mw_stacked_GED.dta", keep(match) // keep matches drops years outside of our enrollment panel

********************************************************************************
**GRAPHING**
********************************************************************************

matrix col1 = J(10,2,.)
matrix col2 = J(10,2,.)
matrix col3 = J(10,3,.)
matrix results = J(10,7,.)

* define treatment vector
global treat F*treat L*treat

*linear combinations of post-reg coeffs
global lincomm3 F3treat - F1treat
global lincomm2 F2treat - F1treat
global lincomm1 F1treat - F1treat
global lincom0 L0treat - F1treat
global lincom1 L1treat - F1treat
global lincom2 L2treat - F1treat
global lincom3 L3treat - F1treat
global lincom4 L4treat - F1treat


preserve
	eststo enrollment:  reghdfe log_eftotl1 $treat , ///
			a(`controlsfe') cluster(`clustervar')
				local dof = e(df_r)
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col1[`i',1] = r(estimate)
						matrix col1[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col1[`i',1] = r(estimate)
						matrix col1[`i++',2] = r(se)
				}
				lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
					matrix col1[`i',1] = r(estimate)
					matrix col1[`i',2] = r(se)
					
	eststo completers:  reghdfe log_completers $treat , a(`controlsfe') cluster(`clustervar') 
				local dof = e(df_r)
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
				lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
					matrix col2[`i',1] = r(estimate)
					matrix col2[`i',2] = r(se)
				

	eststo passers:  reghdfe log_passers $treat , a(`controlsfe') cluster(`clustervar') 
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
				lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
					matrix col3[`i',1] = r(estimate)
					matrix col3[`i',2] = r(se)
					
		matrix col3[1,3] = -3
		matrix col3[2,3] = -2
		matrix col3[3,3] = -1
		matrix col3[4,3] = 0
		matrix col3[5,3] = 1
		matrix col3[6,3] = 2
		matrix col3[7,3] = 3
		matrix col3[8,3] = 4

	matrix results = col1, col2, col3

	clear
	matlist results
	svmat results

	rename results1  coeff_enrl
	rename results2  se_enrl
	rename results3  coeff_comp
	rename results4  se_comp
	rename results5  coeff_pass
	rename results6  se_pass
	rename results7  time

	foreach x in enrl comp pass{
		gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
		gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
	}
	
	//save in tables raw 
	export excel ${output}/tables_raw.xlsx, sheet(ged_log`type'_${dmwlevel}`bal') sheetreplace firstrow(var)
	
		
	//drop dd estimates 
	drop if missing(time)
		
	twoway rarea ci_lower_enrl ci_upper_enrl  time, color(maroon%40) ||  ///
			line coeff_enrl time, lcolor(maroon)  || ///
			rarea ci_lower_comp ci_upper_comp  time, color(teal%45) ||  ///
			line coeff_comp time, lcolor(teal)   ///
			ylab(, grid labsize(small)) yscale() yline(0, lp(dash) lc(gs10)) ///
			xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(2) ///
			order(2 "Enrollment" 4 "Completers" ) size(small)) ytitle() ///
			xtitle("years since federal minimum wage change") legend(region(lstyle(none)))
		graph export "${output}/MWlog_GED_enroll`type'_componly`bal'.pdf", replace
 
	twoway rcap ci_lower_enrl ci_upper_enrl  time, lcolor(maroon%70) ||  ///
			connected coeff_enrl time, lcolor(maroon) mcolor(maroon) || ///
			rcap ci_lower_comp ci_upper_comp  time, lcolor(teal%70) ||  ///
			connected coeff_comp time, lcolor(teal) mcolor(teal)  || ///
			rcap ci_lower_pass ci_upper_pass  time, lcolor(olive_teal%70) ||  ///
			connected coeff_pass time, lcolor(olive_teal) mcolor(olive_teal)  ///
			ylab(, grid labsize(small)) yscale() yline(0, lp(dash) lc(gs10)) ///
			xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(3) ///
			order(2 "Enrollment" 4 "Completers" 6 "Passers") size(small)) ytitle() ///
			xtitle("years since federal minimum wage change") legend(region(lstyle(none)))
		graph export "${output}/MWlog_GED_enroll`type'_all`bal'.pdf", replace
	
restore


//version with controls
if `switch_controls'{
	preserve
		eststo enrl_controls:  reghdfe log_eftotl1 $treat free_cc unemp_rate lnpop_1830  , ///
				a(`controlsfe') cluster(`clustervar')	
					local dof = e(df_r)
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}
					lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
						matrix col1[`i',1] = r(estimate)
						matrix col1[`i',2] = r(se)

		eststo completers:  reghdfe log_completers $treat free_cc unemp_rate lnpop_1830  , ///
				a(`controlsfe') cluster(`clustervar') 
				local dof = e(df_r)
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
				lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
					matrix col2[`i',1] = r(estimate)
					matrix col2[`i',2] = r(se)

		eststo passers:  reghdfe log_passers $treat free_cc unemp_rate lnpop_1830 , ///
				a(`controlsfe') cluster(`clustervar') 
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
				lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
					matrix col3[`i',1] = r(estimate)
					matrix col3[`i',2] = r(se)
				
			matrix col3[1,3] = -3
			matrix col3[2,3] = -2
			matrix col3[3,3] = -1
			matrix col3[4,3] = 0
			matrix col3[5,3] = 1
			matrix col3[6,3] = 2
			matrix col3[7,3] = 3
			matrix col3[8,3] = 4

		matrix results = col1, col2, col3

		clear
		matlist results
		svmat results

		rename results1  coeff_enrl
		rename results2  se_enrl
		rename results3  coeff_comp
		rename results4  se_comp
		rename results5  coeff_pass
		rename results6  se_pass
		rename results7  time

		foreach x in enrl comp pass{
			gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
			gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
		}

	//export table ests
	export excel ${output}/tables_raw.xlsx, sheet(ged_log`type'_cntls_${dmwlevel}`bal') sheetreplace firstrow(var)
			
	//drop dd estimates
	drop if missing(time)
			
	//graphing style things
		grstyle init
		grstyle set plain
		grstyle set size 8pt: subheading axis_title

	twoway rarea ci_lower_enrl ci_upper_enrl  time, color(maroon%40) ||  ///
			line coeff_enrl time, lcolor(maroon) || ///
			rarea ci_lower_comp ci_upper_comp  time, color(teal%45) ||  ///
			line coeff_comp time, lcolor(teal)  ///
			ylab(, grid labsize(small)) yscale() yline(0, lp(dash) lc(gs10)) ///
			xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(2) ///
			order(2 "Enrollment" 4 "Completers" ) size(small)) ytitle() ///
			xtitle("years since federal minimum wage change") legend(region(lstyle(none))) ///
			note(`"controls for log 18-30 year-old population, free CC, and unemployment rates"')
		graph export "${output}/MWlog_GED_enroll`type'_controls_componly`bal'.pdf", replace
		
	twoway rcap ci_lower_enrl ci_upper_enrl  time, lcolor(maroon%70) ||  ///
			connected coeff_enrl time, lcolor(maroon) mcolor(maroon) || ///
			rcap ci_lower_comp ci_upper_comp  time, lcolor(teal%70) ||  ///
			connected coeff_comp time, lcolor(teal) mcolor(teal)  || ///
			rcap ci_lower_pass ci_upper_pass  time, lcolor(olive_teal%70) ||  ///
			connected coeff_pass time, lcolor(olive_teal) mcolor(olive_teal)  ///
			ylab(, grid labsize(small)) yscale() yline(0, lp(dash) lc(gs10)) ///
			xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(3) ///
			order(2 "Enrollment" 4 "Completers" 6 "Passers") size(small)) ytitle() ///
			xtitle("years since federal minimum wage change") legend(region(lstyle(none))) ///
			note(`"controls for log 18-30 year-old population, free CC, and unemployment rates"')
				
		graph export "${output}/MWlog_GED_enroll`type'_controls_all`bal'.pdf", replace
		
			
	restore
}
